package com.rz.htk.rocketmq.producer;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RocketBaseProducer {
    private Logger log = LoggerFactory.getLogger("RocketBaseProducer");

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    public SendResult syncSend(String destination,String msg){
        return rocketMQTemplate.syncSend(destination,msg);
    }

    public void asyncSend(String destination,String msg){
        rocketMQTemplate.asyncSend(destination, msg, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("rocketmq asyncsend success messageId {},message {}",sendResult.getMsgId(),sendResult.toString());
            }

            @Override
            public void onException(Throwable throwable) {
                log.info("rocketmq asyncsend fail excption {}",throwable.getMessage());
            }
        });
    }

    public void sendOneway(String destination,String msg){
        rocketMQTemplate.sendOneWay(destination,msg);
    }
}
